//----------------------------------------------------------------------
//
//  LJUDDelphi.pas
//
//  Delphi wrapper interface for the labjackud dll driver.  Please
//  refer to the labjackud.h c header file and the UE9/U3 User Guide for
//  documentation on function and constant usage.
//
//  Changes from C:
//  -Data type changes:
//     long is LongInt
//     double is Double
//     char * is PAnsiChar (array)
//     unsigned char * is TPByte (array)
//     long * is TPLongInt (array) or pass by reference
//     double * is TPDouble (array) or pass by reference
//  -AddRequest, ePut, eGet are overloadeded so that the pararmeter x1
//   can be acept either a LongInt or a Pointer.  For certain IOTypes, x1
//   can accept a pointer to a AnsiChar or Double array.
//
//  Version History:
//  0.1 - Initial release
//  0.2 - Updated constants and functions to UD driver version 2.67
//  0.3 - Updated constants and functions to UD driver version 3.25
//  0.4 - Updated PChar/Char to PAnsiChar/AnsiChar for newer versions of
//        Delphi
//      - Updated constants and functions to UD driver version 3.35
//
//  support@labjack.com
//  March 28, 2011
//----------------------------------------------------------------------
//
unit LJUDDelphi;

interface

type
  LJ_HANDLE = Integer;
  LJ_ERROR = Integer;
  TPByte = ^Byte;            //Byte pointer
  TPLongInt = ^LongInt;      //LongInt pointer
  TPDouble = ^Double;        //Double pointer

  procedure Close() stdcall;
  function ListAll(DeviceType: LongInt; ConnectionType: LongInt; var pNumFound: LongInt; pSerialNumbers: TPLongInt; pIDs: TPLongInt; pAddresses: TPDouble): LJ_ERROR stdcall;
  function ListAllS(pDeviceType: PAnsiChar; pConnectionType: PAnsiChar; var pNumFound: LongInt; pSerialNumbers: TPLongInt; pIDs: TPLongInt; pAddresses: TPDouble): LJ_ERROR stdcall;

  function OpenLabJack(DeviceType: LongInt; ConnectionType: LongInt; pAddress: PAnsiChar{const pAddress: array of AnsiChar}; FirstFound: LongInt; Var pHandle: LJ_HANDLE): LJ_ERROR stdcall;
  function OpenLabJackS(pDeviceType: PAnsiChar; const pConnectionType: PAnsiChar; pAddress: PAnsiChar; FirstFound: LongInt; var pHandle: LJ_HANDLE): LJ_ERROR stdcall;

  function AddRequest(LJ_HANDLE: LongInt; IOType: LongInt; Channel: LongInt; Value: Double; x1: LongInt; UserData: Double): LJ_ERROR stdcall; OverLoad;
  function AddRequest(LJ_HANDLE: LongInt; IOType: LongInt; Channel: LongInt; Value: Double; x1: Pointer; UserData: Double): LJ_ERROR stdcall; OverLoad;
  function AddRequestS(LJ_HANDLE: LongInt; pIOType: PAnsiChar; Channel: LongInt; Value: Double; x1: LongInt; UserData: Double): LJ_ERROR stdcall;
  function AddRequestSS(LJ_HANDLE: LongInt; pIOType: PAnsiChar; pChannel: PAnsiChar; Value: Double; x1: LongInt; UserData: Double): LJ_ERROR stdcall;

  function Go: LJ_ERROR; stdcall;
  function GoOne(Handle: LJ_HANDLE): LJ_ERROR stdcall;

  function eGet(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; var pValue: Double; x1: LongInt): LJ_ERROR stdcall; Overload;
  function eGet(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; var pValue: Double; x1: Pointer): LJ_ERROR stdcall; Overload;
  function eGetS(Handle: LJ_HANDLE; pIOType: PAnsiChar; Channel: LongInt; var pValue: Double; x1: LongInt): LJ_ERROR stdcall;
  function eGetSS(Handle: LJ_HANDLE; pIOType: PAnsiChar; const spChannel: PAnsiChar; var pValue: Double; x1: LongInt): LJ_ERROR stdcall;

  function ePut(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; Value: Double; x1: LongInt): LJ_ERROR stdcall; OverLoad;
  function ePut(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; Value: Double; x1: Pointer): LJ_ERROR stdcall; OverLoad;
  function ePutS(Handle: LJ_HANDLE; pIOType: PAnsiChar; Channel: LongInt; Value: Double; x1: LongInt): LJ_ERROR stdcall;
  function ePutSS(Handle: LJ_HANDLE; pIOType: PAnsiChar; pChannel: PAnsiChar; Value: Double; x1: LongInt): LJ_ERROR stdcall;

  function eGet_DblArray(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; var pValue, x1: TPDouble): LJ_ERROR stdcall;
  function eGet_U8Array(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; var pValue: Double; x1: TPByte): LJ_ERROR stdcall;
  function eGetS_DblArray(Handle: LJ_HANDLE; pIOType: PAnsiChar; Channel: LongInt; var pValue: Double; x1: TPDouble): LJ_ERROR stdcall;
  function eGetS_U8Array(Handle: LJ_HANDLE; pIOType: PAnsiChar; Channel: LongInt; var pValue: Double; x1: TPByte): LJ_ERROR stdcall;
  function eGetSS_DblArray(Handle: LJ_HANDLE; pIOType: PAnsiChar; pChannel: PAnsiChar; var pValue: Double; x1: TPDouble): LJ_ERROR stdcall;
  function eGetSS_U8Array(Handle: LJ_HANDLE; pIOType: PAnsiChar; pChannel: PAnsiChar; var pValue: Double; x1: TPByte): LJ_ERROR stdcall;

  function GetResult(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; var pValue: Double): LJ_ERROR stdcall;
  function GetResultS(Handle: LJ_HANDLE; const pIOType: array of AnsiChar; Channel: LongInt; var pValue: Double): LJ_ERROR stdcall;
  function GetResultSS(Handle: LJ_HANDLE; const pIOType: array of AnsiChar; const pChannel: array of AnsiChar; var pValue: Double): LJ_ERROR stdcall;

  function GetFirstResult(Handle: LJ_HANDLE; var pIOType: LongInt; var pChannel: LongInt; var pValue: Double; var px1: LongInt; var pUserData: Double): LJ_ERROR stdcall;
  function GetNextResult(Handle: LJ_HANDLE; var pIOType: LongInt; var pChannel: LongInt; var pValue: Double; var px1: LongInt; var pUserData: Double): LJ_ERROR stdcall;

  function eAIN(Handle: LJ_HANDLE; ChannelP: LongInt; ChannelN: LongInt; var Voltage: Double; Range: LongInt; Resolution: LongInt; Settling: LongInt; Binary: LongInt; Reserved1: LongInt; Reserved2: LongInt): LJ_ERROR stdcall;
  function eDAC(Handle: LJ_HANDLE;  Channel: LongInt; Voltage: Double; Binary: LongInt; Reserved1: LongInt; Reserved2: LongInt): LJ_ERROR stdcall;
  function eDI(Handle: LJ_HANDLE; Channel: LongInt; var State: LongInt): LJ_ERROR stdcall;
  function eDO(Handle: LJ_HANDLE; Channel: LongInt; State: LongInt): LJ_ERROR stdcall;
  function eAddGoGet(Handle: LJ_HANDLE; NumRequests: LongInt; aIOTypes: TPLONGINT; aChannels: TPLONGINT; aValues: TPDOUBLE; ax1s: TPLONGINT; aRequestErrors: TPLONGINT; GoError: TPLONGINT; aResultErrors: TPLONGINT): LJ_ERROR stdcall; OverLoad;
  function eAddGoGet(Handle: LJ_HANDLE; NumRequests: LongInt; aIOTypes: TPLONGINT; aChannels: TPLONGINT; aValues: TPDOUBLE; ax1s: Pointer; aRequestErrors: TPLONGINT; GoError: TPLONGINT; aResultErrors: TPLONGINT): LJ_ERROR stdcall; OverLoad;
  function eTCConfig(Handle: LJ_HANDLE; aEnableTimers: TPLONGINT; aEnableCounters: TPLONGINT; TCPinOffset: LongInt; TimerClockBaseIndex: LongInt; TimerClockDivisor: LongInt; aTimerModes: TPLONGINT; aTimerValues: TPDOUBLE; Reserved1: LongInt; Reserved2: LongInt): LJ_ERROR stdcall;
  function eTCValues(Handle: LJ_HANDLE; aReadTimers: TPLONGINT; aUpdateResetTimers: TPLONGINT; aReadCounters: TPLONGINT; aResetCounters: TPLONGINT; aTimerValues: TPDOUBLE; aCounterValues: TPDOUBLE; Reserved1: LongInt; Reserved2: LongInt): LJ_ERROR stdcall;

  function eModbus(Handle: LJ_HANDLE; readwrite: LongInt; addr: LongInt; size: LongInt; value: TPByte): LJ_ERROR stdcall;

  function ResetLabJack(Handle: LJ_HANDLE): LJ_ERROR stdcall;

  function GetNextError(Handle: LJ_HANDLE; var pIOType: LongInt; var pChannel: LongInt): LJ_ERROR stdcall;
  function GetStreamError(Handle: LJ_HANDLE): LJ_ERROR stdcall;

  function DoubleToStringAddress(Number: Double; pString: PAnsiChar; HexDot: LongInt): LJ_ERROR stdcall;

  function StringToDoubleAddress(pString: PAnsiChar; var pNumber: Double; HexDot: LongInt): LongInt stdcall;
  function StringToConstant(pString: PAnsiChar): LongInt stdcall;

  procedure ErrorToString(ErrorCode: LongInt; pString: PAnsiChar) stdcall;

  function GetDriverVersion: Double stdcall;

  function TCVoltsToTemp(TCType: LongInt; TCVolts: Double; CJTempK: Double; var pTCTempK: Double): LJ_ERROR stdcall;

  function GetThreadID: Cardinal stdcall;

const
  //Version number of the LJUDDelphi wrapper
  LJUDDELPHI_VERSION = 0.4;

  //UD Driver version this was last tested with
  DRIVER_VERSION = 3.35;

  // device types
  LJ_dtUE9: LongInt = 9;
  LJ_dtU3: LongInt = 3;
  LJ_dtU6: LongInt = 6;

  // connection types:
  LJ_ctUSB: LongInt = 1;
  LJ_ctETHERNET: LongInt = 2;
  LJ_ctETHERNET_MB: LongInt = 3; // Modbus over Ethernet, UE9 only.
  LJ_ctETHERNET_DATA_ONLY: LongInt = 4; // Opens data port but not stream port, UE9 only

  LJ_ctUSB_RAW: LongInt = 101;
  LJ_ctETHERNET_RAW: LongInt = 102;

  // io types:
  LJ_ioGET_AIN: LongInt = 10;
  LJ_ioGET_AIN_DIFF: LongInt = 15;
  LJ_ioGET_AIN_ADVANCED: LongInt = 16;

  LJ_ioPUT_AIN_RANGE: LongInt = 2000;
  LJ_ioGET_AIN_RANGE: LongInt = 2001;

  LJ_ioPUT_ANALOG_ENABLE_BIT: LongInt = 2013;
  LJ_ioGET_ANALOG_ENABLE_BIT: LongInt = 2014;

  LJ_ioPUT_ANALOG_ENABLE_PORT: LongInt = 2015;
  LJ_ioGET_ANALOG_ENABLE_PORT: LongInt = 2016;

  LJ_ioPUT_DAC: LongInt = 20;
  LJ_ioPUT_DAC_ENABLE: LongInt = 2002;
  LJ_ioGET_DAC_ENABLE: LongInt = 2003;

  LJ_ioGET_DIGITAL_BIT: LongInt = 30;
  LJ_ioGET_DIGITAL_BIT_DIR: LongInt = 31;
  LJ_ioGET_DIGITAL_BIT_STATE: LongInt = 32;

  LJ_ioGET_DIGITAL_PORT: LongInt = 35;
  LJ_ioGET_DIGITAL_PORT_DIR: LongInt = 36;
  LJ_ioGET_DIGITAL_PORT_STATE: LongInt = 37;

  LJ_ioPUT_DIGITAL_BIT: LongInt = 40;
  LJ_ioPUT_DIGITAL_PORT: LongInt = 45;

  LJ_ioPUT_WAIT: LongInt = 70;

  LJ_ioGET_COUNTER: LongInt = 50;

  LJ_ioPUT_COUNTER_ENABLE: LongInt = 2008; 
  LJ_ioGET_COUNTER_ENABLE: LongInt = 2009; 

  LJ_ioPUT_COUNTER_RESET: LongInt = 2012;

  LJ_ioGET_TIMER: LongInt = 60;

  LJ_ioPUT_TIMER_VALUE: LongInt = 2006;
  LJ_ioPUT_TIMER_MODE: LongInt = 2004; 
  LJ_ioGET_TIMER_MODE: LongInt = 2005;

  LJ_ioSHT_GET_READING: LongInt = 500;

  LJ_ioSPI_COMMUNICATION: LongInt = 503;

  LJ_ioI2C_COMMUNICATION: LongInt = 504;
  LJ_ioASYNCH_COMMUNICATION: LongInt = 505;
  LJ_ioTDAC_COMMUNICATION: LongInt = 506;

  LJ_ioPIN_CONFIGURATION_RESET: LongInt = 2017;

  LJ_ioRAW_OUT: LongInt = 100; 
  LJ_ioRAW_IN: LongInt = 101;

  LJ_ioRAWMB_OUT: LongInt = 104;
  LJ_ioRAWMB_IN: LongInt = 105;

  LJ_ioSET_DEFAULTS: LongInt = 103;

  LJ_ioADD_STREAM_CHANNEL: LongInt = 200;
  LJ_ioADD_STREAM_CHANNEL_DIFF: LongInt = 206;

  LJ_ioCLEAR_STREAM_CHANNELS: LongInt = 201;
  LJ_ioSTART_STREAM: LongInt = 202;
  LJ_ioSTOP_STREAM: LongInt = 203;

  LJ_ioADD_STREAM_DAC: LongInt = 207;

  LJ_ioGET_STREAM_DATA: LongInt = 204;
  LJ_ioSET_STREAM_CALLBACK: LongInt = 205;

  LJ_ioBUZZER: LongInt = 300;

  LJ_ioSET_EVENT_CALLBACK: LongInt = 400;

  // possible EventCodes
  LJ_ecDISCONNECT: LongInt = 1;
  LJ_ecRECONNECT: LongInt = 2;
  LJ_ecSTREAMERROR: LongInt = 4;

  // config iotypes:
  LJ_ioPUT_CONFIG: LongInt = 1000;
  LJ_ioGET_CONFIG: LongInt = 1001;

  // channel numbers used for CONFIG types:
  // UE9 + U3 + U6
  LJ_chLOCALID: LongInt = 0;
  LJ_chHARDWARE_VERSION: LongInt = 10;
  LJ_chSERIAL_NUMBER: LongInt = 12;
  LJ_chFIRMWARE_VERSION: LongInt = 11; 
  LJ_chBOOTLOADER_VERSION: LongInt = 15;
  LJ_chPRODUCTID: LongInt = 8;

  // UE9 specific:
  LJ_chCOMM_POWER_LEVEL: LongInt = 1;
  LJ_chIP_ADDRESS: LongInt = 2;
  LJ_chGATEWAY: LongInt = 3;
  LJ_chSUBNET: LongInt = 4;
  LJ_chPORTA: LongInt = 5;
  LJ_chPORTB: LongInt = 6;
  LJ_chDHCP: LongInt = 7;
  LJ_chMACADDRESS: LongInt = 9;
  LJ_chCOMM_FIRMWARE_VERSION: LongInt = 11;
  LJ_chCONTROL_POWER_LEVEL: LongInt = 13; 
  LJ_chCONTROL_FIRMWARE_VERSION: LongInt = 14; 
  LJ_chCONTROL_BOOTLOADER_VERSION: LongInt = 15;
  LJ_chCONTROL_RESET_SOURCE: LongInt = 16;
  LJ_chUE9_PRO: LongInt = 19;

  // U3 only:
  LJ_chLED_STATE: LongInt = 17;
  LJ_chSDA_SCL: LongInt = 18;
  LJ_chU3HV: LongInt = 22;

  // U6 only
  LJ_chU6_PRO: LongInt = 23;

  // Driver related:
  LJ_chCOMMUNICATION_TIMEOUT: LongInt = 20;
  LJ_chSTREAM_COMMUNICATION_TIMEOUT: LongInt = 21;

  LJ_chCAL_CONSTANTS: LongInt = 400;
  LJ_chUSER_MEM: LongInt = 402;

  LJ_chUSB_STRINGS: LongInt = 404;

  // timer/counter related
  LJ_chNUMBER_TIMERS_ENABLED: LongInt = 1000; 
  LJ_chTIMER_CLOCK_BASE: LongInt = 1001;
  LJ_chTIMER_CLOCK_DIVISOR: LongInt = 1002;
  LJ_chTIMER_COUNTER_PIN_OFFSET: LongInt = 1003;

  // AIn related
  LJ_chAIN_RESOLUTION: LongInt = 2000; 
  LJ_chAIN_SETTLING_TIME: LongInt = 2001;
  LJ_chAIN_BINARY: LongInt = 2002; 

  // DAC related
  LJ_chDAC_BINARY: LongInt = 3000; 

  // SHT related
  LJ_chSHT_TEMP: LongInt = 5000;
  LJ_chSHT_RH: LongInt = 5001;
  LJ_chSHT_DATA_CHANNEL: LongInt = 5002;
  LJ_chSHT_CLOCK_CHANNEL: LongInt = 5003;

  // SPI related
  LJ_chSPI_AUTO_CS: LongInt = 5100;
  LJ_chSPI_DISABLE_DIR_CONFIG: LongInt = 5101;
  LJ_chSPI_MODE: LongInt = 5102;
  LJ_chSPI_CLOCK_FACTOR: LongInt = 5103;
  LJ_chSPI_MOSI_PINNUM: LongInt = 5104;
  LJ_chSPI_MISO_PINNUM: LongInt = 5105;
  LJ_chSPI_CLK_PINNUM: LongInt = 5106;
  LJ_chSPI_CS_PINNUM: LongInt = 5107;

  // I2C related :
  // used with LJ_ioPUT_CONFIG
  LJ_chI2C_ADDRESS_BYTE: LongInt = 5108;
  LJ_chI2C_SCL_PIN_NUM: LongInt = 5109;
  LJ_chI2C_SDA_PIN_NUM: LongInt = 5110;
  LJ_chI2C_OPTIONS: LongInt = 5111;
  LJ_chI2C_SPEED_ADJUST: LongInt = 5112;

  // used with LJ_ioI2C_COMMUNICATION :
  LJ_chI2C_READ: LongInt = 5113;
  LJ_chI2C_WRITE: LongInt = 5114;
  LJ_chI2C_GET_ACKS: LongInt = 5115;
  LJ_chI2C_WRITE_READ: LongInt = 5130;

  // Asynch related
  // Used with LJ_ioASYNCH_COMMUNICATION
  LJ_chASYNCH_RX: LongInt = 5117;
  LJ_chASYNCH_TX: LongInt = 5118;
  LJ_chASYNCH_FLUSH: LongInt = 5128;
  LJ_chASYNCH_ENABLE: LongInt = 5129;

  // Used with LJ_ioPUT_CONFIG and LJ_ioGET_CONFIG
  LJ_chASYNCH_BAUDFACTOR: LongInt = 5127;

  // LJ TickDAC related :
  LJ_chTDAC_SCL_PIN_NUM: LongInt = 5119; // UE9 + U3:  Used with LJ_ioPUT_CONFIG

  // Used with LJ_ioTDAC_COMMUNICATION
  LJ_chTDAC_SERIAL_NUMBER: LongInt = 5120; // UE9 + U3: Read only
  LJ_chTDAC_READ_USER_MEM: LongInt = 5121; // UE9 + U3
  LJ_chTDAC_WRITE_USER_MEM: LongInt = 5122; // UE9 + U3
  LJ_chTDAC_READ_CAL_CONSTANTS: LongInt = 5123; // UE9 + U3
  LJ_chTDAC_WRITE_CAL_CONSTANTS: LongInt = 5124; // UE9 + U3
  LJ_chTDAC_UPDATE_DACA: LongInt = 5125; // UE9 + U3
  LJ_chTDAC_UPDATE_DACB: LongInt = 5126; // UE9 + U3

  // stream related.  Note, Putting to any of these values will stop any running streams.
  LJ_chSTREAM_SCAN_FREQUENCY: LongInt = 4000;
  LJ_chSTREAM_BUFFER_SIZE: LongInt = 4001;
  LJ_chSTREAM_CLOCK_OUTPUT: LongInt = 4002;
  LJ_chSTREAM_EXTERNAL_TRIGGER: LongInt = 4003;
  LJ_chSTREAM_WAIT_MODE: LongInt = 4004;
  LJ_chSTREAM_DISABLE_AUTORECOVERY: LongInt = 4005; // U3
  LJ_chSTREAM_SAMPLES_PER_PACKET: LongInt = 4108;
  LJ_chSTREAM_READS_PER_SECOND: LongInt = 4109;
  LJ_chAIN_STREAM_SETTLING_TIME: LongInt = 4110; // U6

  // readonly stream related
  LJ_chSTREAM_BACKLOG_COMM: LongInt = 4105;
  LJ_chSTREAM_BACKLOG_CONTROL: LongInt = 4106;
  LJ_chSTREAM_BACKLOG_UD: LongInt = 4107;

  // special channel #'s
  LJ_chALL_CHANNELS: LongInt = -1;
  LJ_INVALID_CONSTANT: LongInt = -999;

  //Thermocouple Type constants.
  LJ_ttB: LongInt = 6001;
  LJ_ttE: LongInt = 6002;
  LJ_ttJ: LongInt = 6003;
  LJ_ttK: LongInt = 6004;
  LJ_ttN: LongInt = 6005;
  LJ_ttR: LongInt = 6006;
  LJ_ttS: LongInt = 6007;
  LJ_ttT: LongInt = 6008;

  // other constants:
  // ranges (not all are supported by all devices):
  LJ_rgBIP20V: LongInt = 1;
  LJ_rgBIP10V: LongInt = 2;
  LJ_rgBIP5V: LongInt = 3;
  LJ_rgBIP4V: LongInt = 4;
  LJ_rgBIP2P5V: LongInt = 5;
  LJ_rgBIP2V: LongInt = 6;
  LJ_rgBIP1P25V: LongInt = 7;
  LJ_rgBIP1V: LongInt = 8;
  LJ_rgBIPP625V: LongInt = 9;
  LJ_rgBIPP1V: LongInt = 10;
  LJ_rgBIPP01V: LongInt = 11;

  LJ_rgUNI20V: LongInt = 101;
  LJ_rgUNI10V: LongInt = 102;
  LJ_rgUNI5V: LongInt = 103;
  LJ_rgUNI4V: LongInt = 104;
  LJ_rgUNI2P5V: LongInt = 105;
  LJ_rgUNI2V: LongInt = 106;
  LJ_rgUNI1P25V: LongInt = 107;
  LJ_rgUNI1V: LongInt = 108;
  LJ_rgUNIP625V: LongInt = 109;
  LJ_rgUNIP5V: LongInt = 110;
  LJ_rgUNIP25V: LongInt = 112;
  LJ_rgUNIP3125V: LongInt = 111;
  LJ_rgUNIP025V: LongInt = 113;
  LJ_rgUNIP0025V: LongInt = 114;

  // timer modes:
  LJ_tmPWM16: LongInt = 0;
  LJ_tmPWM8: LongInt = 1;
  LJ_tmRISINGEDGES32: LongInt = 2;
  LJ_tmFALLINGEDGES32: LongInt = 3;
  LJ_tmDUTYCYCLE: LongInt = 4;
  LJ_tmFIRMCOUNTER: LongInt = 5;
  LJ_tmFIRMCOUNTERDEBOUNCE: LongInt = 6;
  LJ_tmFREQOUT: LongInt = 7;
  LJ_tmQUAD: LongInt = 8;
  LJ_tmTIMERSTOP : LongInt= 9;
  LJ_tmSYSTIMERLOW: LongInt = 10;
  LJ_tmSYSTIMERHIGH: LongInt = 11;
  LJ_tmRISINGEDGES16: LongInt = 12;
  LJ_tmFALLINGEDGES16: LongInt = 13;
  LJ_tmLINETOLINE: LongInt = 14;

  // timer clocks:
  LJ_tc750KHZ: LongInt = 0;
  LJ_tcSYS: LongInt = 1;

  LJ_tc2MHZ: LongInt = 10;
  LJ_tc6MHZ: LongInt = 11;
  LJ_tc24MHZ: LongInt = 12;
  LJ_tc500KHZ_DIV: LongInt = 13;
  LJ_tc2MHZ_DIV: LongInt = 14;
  LJ_tc6MHZ_DIV: LongInt = 15;
  LJ_tc24MHZ_DIV: LongInt = 16;

  LJ_tc4MHZ: LongInt = 20;
  LJ_tc12MHZ: LongInt = 21;
  LJ_tc48MHZ: LongInt = 22;
  LJ_tc1MHZ_DIV: LongInt = 23;
  LJ_tc4MHZ_DIV: LongInt = 24;
  LJ_tc12MHZ_DIV: LongInt = 25;
  LJ_tc48MHZ_DIV: LongInt = 26;

  // stream wait modes
  LJ_swNONE: LongInt = 1;
  LJ_swALL_OR_NONE: LongInt = 2;
  LJ_swPUMP: LongInt = 11;
  LJ_swSLEEP: LongInt = 12;

  // BETA CONSTANTS
  // Please note that specific usage of these constants and their values might change

  // SWDT related
  LJ_ioSWDT_CONFIG: LongInt = 507; // UE9 & U3 - Use with LJ_chSWDT_ENABLE or LJ_chSWDT_DISABLE
  LJ_ioSWDT_STROKE: LongInt = 508; // UE9 - Used when SWDT_STRICT_ENABLE is turned on to renew the watchdog.

  LJ_chSWDT_ENABLE: LongInt = 5200; // UE9 & U3 - used with LJ_ioSWDT_CONFIG to enable watchdog.  Value paramter is number of seconds to trigger
  LJ_chSWDT_DISABLE: LongInt = 5201; // UE9 & U3 - used with LJ_ioSWDT_CONFIG to enable watchdog.

  // Used with LJ_io_PUT_CONFIG
  LJ_chSWDT_RESET_DEVICE: LongInt = 5202; // U3 - Reset U3 on watchdog reset.  Write only.
  LJ_chSWDT_RESET_COMM: LongInt = 5203; // UE9 - Reset Comm on watchdog reset.  Write only.
  LJ_chSWDT_RESET_CONTROL: LongInt = 5204; // UE9 - Reset Control on watchdog trigger.  Write only.
  LJ_chSWDT_UDPATE_DIOA: LongInt = 5205; // UE9 & U3 - Update DIO0 settings after reset.  Write only.
  LJ_chSWDT_UPDATE_DIOB: LongInt = 5206; // UE9 - Update DIO1 settings after reset.  Write only.
  LJ_chSWDT_DIOA_CHANNEL: LongInt = 5207; // UE9 & U3 - DIO0 channel to be set after reset.  Write only.
  LJ_chSWDT_DIOA_STATE: LongInt = 5208; // UE9 & U3 - DIO0 state to be set after reset.  Write only.
  LJ_chSWDT_DIOB_CHANNEL: LongInt = 5209; // UE9 - DIO1 channel to be set after reset.  Write only.
  LJ_chSWDT_DIOB_STATE: LongInt = 5210; // UE9 - DIO0 state to be set after reset.  Write only.
  LJ_chSWDT_UPDATE_DAC0: LongInt = 5211; // UE9 - Update DAC0 settings after reset.  Write only.
  LJ_chSWDT_UPDATE_DAC1: LongInt = 5212; // UE9 - Update DAC1 settings after reset.  Write only.
  LJ_chSWDT_DAC0: LongInt = 5213; // UE9 - voltage to set DAC0 at on watchdog reset.  Write only.
  LJ_chSWDT_DAC1: LongInt = 5214; // UE9 - voltage to set DAC1 at on watchdog reset.  Write only.
  LJ_chSWDT_DAC_ENABLE: LongInt = 5215; // UE9 - Enable DACs on watchdog reset.  Default is true.  Both DACs are enabled or disabled togeather.  Write only.
  LJ_chSWDT_STRICT_ENABLE: LongInt = 5216; // UE9 - Watchdog will only renew with LJ_ioSWDT_STROKE command.
  LJ_chSWDT_INITIAL_ROLL_TIME: LongInt = 5217; // UE9 - Watchdog timer for the first cycle when powered on, after watchdog triggers a reset the normal value is used.  Set to 0 to disable.

  // END BETA CONSTANTS

  // error codes
  LJE_NOERROR: LJ_ERROR = 0;

  LJE_INVALID_CHANNEL_NUMBER: LJ_ERROR = 2;
  LJE_INVALID_RAW_INOUT_PARAMETER: LJ_ERROR = 3;
  LJE_UNABLE_TO_START_STREAM: LJ_ERROR = 4;
  LJE_UNABLE_TO_STOP_STREAM: LJ_ERROR = 5;
  LJE_NOTHING_TO_STREAM: LJ_ERROR = 6;
  LJE_UNABLE_TO_CONFIG_STREAM: LJ_ERROR = 7;
  LJE_BUFFER_OVERRUN: LJ_ERROR = 8;
  LJE_STREAM_NOT_RUNNING: LJ_ERROR = 9;
  LJE_INVALID_PARAMETER: LJ_ERROR = 10;
  LJE_INVALID_STREAM_FREQUENCY: LJ_ERROR = 11;
  LJE_INVALID_AIN_RANGE: LJ_ERROR = 12;
  LJE_STREAM_CHECKSUM_ERROR: LJ_ERROR = 13;
  LJE_STREAM_COMMAND_ERROR: LJ_ERROR = 14;
  LJE_STREAM_ORDER_ERROR: LJ_ERROR = 15;
  LJE_AD_PIN_CONFIGURATION_ERROR: LJ_ERROR = 16;
  LJE_REQUEST_NOT_PROCESSED: LJ_ERROR = 17;

  // U3 & U6 Specific Errors
  LJE_SCRATCH_ERROR: LJ_ERROR = 19;
  LJE_DATA_BUFFER_OVERFLOW: LJ_ERROR = 20;
  LJE_ADC0_BUFFER_OVERFLOW: LJ_ERROR = 21;
  LJE_FUNCTION_INVALID: LJ_ERROR = 22;
  LJE_SWDT_TIME_INVALID: LJ_ERROR = 23;
  LJE_FLASH_ERROR: LJ_ERROR = 24;
  LJE_STREAM_IS_ACTIVE: LJ_ERROR = 25;
  LJE_STREAM_TABLE_INVALID: LJ_ERROR = 26;
  LJE_STREAM_CONFIG_INVALID: LJ_ERROR = 27;
  LJE_STREAM_BAD_TRIGGER_SOURCE: LJ_ERROR = 28;
  LJE_STREAM_INVALID_TRIGGER: LJ_ERROR = 30;
  LJE_STREAM_ADC0_BUFFER_OVERFLOW: LJ_ERROR = 31;
  LJE_STREAM_SAMPLE_NUM_INVALID: LJ_ERROR = 33;
  LJE_STREAM_BIPOLAR_GAIN_INVALID: LJ_ERROR = 34;
  LJE_STREAM_SCAN_RATE_INVALID: LJ_ERROR = 35;
  LJE_TIMER_INVALID_MODE: LJ_ERROR = 36;
  LJE_TIMER_QUADRATURE_AB_ERROR: LJ_ERROR = 37;
  LJE_TIMER_QUAD_PULSE_SEQUENCE: LJ_ERROR = 38;
  LJE_TIMER_BAD_CLOCK_SOURCE: LJ_ERROR = 39;
  LJE_TIMER_STREAM_ACTIVE: LJ_ERROR = 40;
  LJE_TIMER_PWMSTOP_MODULE_ERROR: LJ_ERROR = 41;
  LJE_TIMER_SEQUENCE_ERROR: LJ_ERROR = 42;
  LJE_TIMER_SHARING_ERROR: LJ_ERROR = 43;
  LJE_TIMER_LINE_SEQUENCE_ERROR: LJ_ERROR = 44;
  LJE_EXT_OSC_NOT_STABLE: LJ_ERROR = 45;
  LJE_INVALID_POWER_SETTING: LJ_ERROR = 46;
  LJE_PLL_NOT_LOCKED: LJ_ERROR = 47;
  LJE_INVALID_PIN: LJ_ERROR = 48;
  LJE_IOTYPE_SYNCH_ERROR: LJ_ERROR = 49;
  LJE_INVALID_OFFSET: LJ_ERROR = 50;
  LJE_FEEDBACK_IOTYPE_NOT_VALID: LJ_ERROR = 51;
  LJE_CANT_CONFIGURE_PIN_FOR_ANALOG: LJ_ERROR = 67;
  LJE_CANT_CONFIGURE_PIN_FOR_DIGITAL: LJ_ERROR = 68;
  LJE_TC_PIN_OFFSET_MUST_BE_4_TO_8: LJ_ERROR = 70;
  LJE_INVALID_DIFFERENTIAL_CHANNEL: LJ_ERROR = 71;
  LJE_DSP_SIGNAL_OUT_OF_RANGE: LJ_ERROR = 72;

  // Other errors
  LJE_SHT_CRC: LJ_ERROR = 52;
  LJE_SHT_MEASREADY: LJ_ERROR = 53;
  LJE_SHT_ACK: LJ_ERROR = 54;
  LJE_SHT_SERIAL_RESET: LJ_ERROR = 55;
  LJE_SHT_COMMUNICATION: LJ_ERROR = 56;

  LJE_AIN_WHILE_STREAMING: LJ_ERROR = 57;

  LJE_STREAM_TIMEOUT: LJ_ERROR = 58;
  LJE_STREAM_CONTROL_BUFFER_OVERFLOW: LJ_ERROR = 59;
  LJE_STREAM_SCAN_OVERLAP: LJ_ERROR = 60;
  LJE_FIRMWARE_VERSION_IOTYPE: LJ_ERROR = 61;
  LJE_FIRMWARE_VERSION_CHANNEL: LJ_ERROR = 62;
  LJE_FIRMWARE_VERSION_VALUE: LJ_ERROR = 63;
  LJE_HARDWARE_VERSION_IOTYPE: LJ_ERROR = 64;
  LJE_HARDWARE_VERSION_CHANNEL: LJ_ERROR = 65;
  LJE_HARDWARE_VERSION_VALUE: LJ_ERROR = 66;

  LJE_LJTDAC_ACK_ERROR: LJ_ERROR = 69;

  LJE_STREAM_INVALID_CONNECTION: LJ_ERROR = 73;

  LJE_MIN_GROUP_ERROR: LJ_ERROR = 1000;

  LJE_UNKNOWN_ERROR: LJ_ERROR = 1001;
  LJE_INVALID_DEVICE_TYPE: LJ_ERROR = 1002;
  LJE_INVALID_HANDLE: LJ_ERROR = 1003;
  LJE_DEVICE_NOT_OPEN: LJ_ERROR = 1004;
  LJE_NO_DATA_AVAILABLE: LJ_ERROR = 1005;
  LJE_NO_MORE_DATA_AVAILABLE: LJ_ERROR = 1006;
  LJE_LABJACK_NOT_FOUND: LJ_ERROR = 1007;
  LJE_COMM_FAILURE: LJ_ERROR = 1008;
  LJE_CHECKSUM_ERROR: LJ_ERROR = 1009;
  LJE_DEVICE_ALREADY_OPEN: LJ_ERROR = 1010;
  LJE_COMM_TIMEOUT: LJ_ERROR = 1011;
  LJE_USB_DRIVER_NOT_FOUND: LJ_ERROR = 1012;
  LJE_INVALID_CONNECTION_TYPE: LJ_ERROR = 1013;
  LJE_INVALID_MODE: LJ_ERROR = 1014;
  LJE_DEVICE_NOT_CONNECTED: LJ_ERROR = 1015;

  LJE_DISCONNECT: LJ_ERROR = 2000;
  LJE_RECONNECT: LJ_ERROR = 2001;

  // and an area for your own codes.  This area won't ever be used for LabJack codes.
  LJE_MIN_USER_ERROR: LJ_ERROR = 3000;
  LJE_MAX_USER_ERROR: LJ_ERROR = 3999;

  // warning are negative
  LJE_DEVICE_NOT_CALIBRATED: LJ_ERROR = -1;
  LJE_UNABLE_TO_READ_CALDATA: LJ_ERROR = -2;

  // depreciated constants
  LJ_ioANALOG_INPUT: LongInt = 10;
  LJ_ioANALOG_OUTPUT: LongInt = 20;
  LJ_ioDIGITAL_BIT_IN: LongInt = 30;
  LJ_ioDIGITAL_PORT_IN: LongInt = 35;
  LJ_ioDIGITAL_BIT_OUT: LongInt = 40;
  LJ_ioDIGITAL_PORT_OUT: LongInt = 45;
  LJ_ioCOUNTER: LongInt = 50;
  LJ_ioTIMER: LongInt = 60;
  LJ_ioPUT_COUNTER_MODE: LongInt = 2010;
  LJ_ioGET_COUNTER_MODE: LongInt = 2011;
  LJ_ioGET_TIMER_VALUE: LongInt = 2007;
  LJ_ioCYCLE_PORT: LongInt = 102;
  LJ_chTIMER_CLOCK_CONFIG: LongInt = 1001;
  LJ_ioPUT_CAL_CONSTANTS: LongInt = 400;
  LJ_ioGET_CAL_CONSTANTS: LongInt = 401;
  LJ_ioPUT_USER_MEM: LongInt = 402;
  LJ_ioGET_USER_MEM: LongInt = 403;
  LJ_ioPUT_USB_STRINGS: LongInt = 404;
  LJ_ioGET_USB_STRINGS: LongInt = 405;
  LJ_ioSHT_DATA_CHANNEL: LongInt = 501;
  LJ_ioSHT_CLOCK_CHANNEL: LongInt = 502;
  LJ_chI2C_ADDRESS: LongInt = 5108;
  LJ_chASYNCH_CONFIG: LongInt = 5116;
  LJ_rgUNIP500V: LongInt = 110;
  LJ_ioENABLE_POS_PULLDOWN: LongInt = 2018;
  LJ_ioENABLE_NEG_PULLDOWN: LongInt = 2019;
  LJ_rgAUTO: LongInt = 0;

implementation
  procedure Close; external 'labjackud.dll';
  function ListAll; external 'labjackud.dll';
  function ListAllS; external 'labjackud.dll';
  function OpenLabJack; external 'labjackud.dll';
  function OpenLabJackS; external 'labjackud.dll';
  function AddRequest(LJ_HANDLE: LongInt; IOType: LongInt; Channel: LongInt; Value: Double; x1: LongInt; UserData: Double): LJ_ERROR stdcall; external 'labjackud.dll';
  function AddRequest(LJ_HANDLE: LongInt; IOType: LongInt; Channel: LongInt; Value: Double; x1: Pointer; UserData: Double): LJ_ERROR stdcall; external 'labjackud.dll';
  function AddRequestS; external 'labjackud.dll';
  function AddRequestSS; external 'labjackud.dll';
  function Go; external 'labjackud.dll';
  function GoOne; external 'labjackud.dll';
  function eGet(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; var pValue: Double; x1: LongInt): LJ_ERROR stdcall; external 'labjackud.dll';
  function eGet(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; var pValue: Double; x1: Pointer): LJ_ERROR stdcall; external 'labjackud.dll';
  function eGetS; external 'labjackud.dll';
  function eGetSS; external 'labjackud.dll';
  function ePut(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; Value: Double; x1: LongInt): LJ_ERROR stdcall; external 'labjackud.dll';
  function ePut(Handle: LJ_HANDLE; IOType: LongInt; Channel: LongInt; Value: Double; x1: Pointer): LJ_ERROR stdcall; external 'labjackud.dll';
  function ePutS; external 'labjackud.dll';
  function ePutSS; external 'labjackud.dll';
  function eGet_DblArray; external 'labjackud.dll';
  function eGet_U8Array; external 'labjackud.dll';
  function eGetS_DblArray; external 'labjackud.dll';
  function eGetS_U8Array; external 'labjackud.dll';
  function eGetSS_DblArray; external 'labjackud.dll';
  function eGetSS_U8Array; external 'labjackud.dll';
  function GetResult; external 'labjackud.dll';
  function GetResultS; external 'labjackud.dll';
  function GetResultSS; external 'labjackud.dll';
  function GetFirstResult; external 'labjackud.dll';
  function GetNextResult; external 'labjackud.dll';
  function eAIN; external 'labjackud.dll';
  function eDAC; external 'labjackud.dll';
  function eDI; external 'labjackud.dll';
  function eDO; external 'labjackud.dll';
  function eAddGoGet(Handle: LJ_HANDLE; NumRequests: LongInt; aIOTypes: TPLONGINT; aChannels: TPLONGINT; aValues: TPDOUBLE; ax1s: TPLONGINT; aRequestErrors: TPLONGINT; GoError: TPLONGINT; aResultErrors: TPLONGINT): LJ_ERROR stdcall; external 'labjackud.dll';
  function eAddGoGet(Handle: LJ_HANDLE; NumRequests: LongInt; aIOTypes: TPLONGINT; aChannels: TPLONGINT; aValues: TPDOUBLE; ax1s: Pointer; aRequestErrors: TPLONGINT; GoError: TPLONGINT; aResultErrors: TPLONGINT): LJ_ERROR stdcall; external 'labjackud.dll';
  function eTCConfig; external 'labjackud.dll';
  function eTCValues; external 'labjackud.dll';
  function eModbus; external 'labjackud.dll';
  function ResetLabJack; external 'labjackud.dll';
  function GetNextError; external 'labjackud.dll';
  function GetStreamError; external 'labjackud.dll';
  function DoubleToStringAddress; external 'labjackud.dll';
  function StringToDoubleAddress; external 'labjackud.dll';
  function StringToConstant; external 'labjackud.dll';
  procedure ErrorToString; external 'labjackud.dll';
  function GetDriverVersion; external 'labjackud.dll';
  function TCVoltsToTemp; external 'labjackud.dll';
  function GetThreadID; external 'labjackud.dll';
end.

